﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SampleResizebleItem.Models;
using SampleResizebleItem.Resources;
using SampleResizebleItem.ViewModels;
using VariableSizedWrapGrid = SampleResizebleItem.Controls.VariableSizedWrapGrid;

namespace SampleResizebleItem
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        private ViewModels.MainViewModel _viewModel;
        public MainPage()
        {
            InitializeComponent();
            _viewModel = new MainViewModel();
            this.DataContext = _viewModel;
            this.Loaded += MainPage_Loaded;
            App.EventAggregator.GetEvent<Events.EventGetFlickr>().Subscribe(Recived);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            _viewModel.LoadFlickr();
        }

        void Recived(IEnumerable<ModelImages> input)
        {
             foreach (var item in input)
            {
                var grd = new Grid();
                grd.Margin = new Thickness(12,0,0,12);
                var variable = new VariableSizedWrapGrid();
                var img = new Image();
                var bi = new BitmapImage(new Uri(item.ImageLarge, UriKind.Absolute));
                img.Stretch = Stretch.Fill;
                img.Source = bi;
                grd.SetValue(Controls.VariableSizedWrapGrid.RowSpanProperty, item.RowSpan);
                grd.SetValue(Controls.VariableSizedWrapGrid.ColumnSpanProperty, item.ColumnSpan);
                grd.Children.Add(img);
                variableSizedWrapGrid.Children.Add(grd);
            }
        }

    }
}